package com.mytest.thread;

public class VolatileVisibilityTest1 {

    public static boolean flag = false;

    public static void main(String[] args) {

        //注释掉Thread.sleep则永远不会输出over，不注掉则会输出
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                while (!flag){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("over");
            }

        }).start();*/
        //注释掉Thread.sleep则永远不会输出over，不注掉则会输出
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (flag==true){
                        break;
                    }
                }
                System.out.println("over");
            }

        }).start();*/

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                flag = true;
            }
        }).start();
    }

}
